package nl.ceasaro.poker.texasHoldEm.game.mvc;

import nl.ceasaro.jeubelix.mvc.AbstractJeubelixController;
import nl.ceasaro.poker.texasHoldEm.game.TexasGameManager;
import nl.ceasaro.poker.texasHoldEm.game.TexasPlayer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA.
 * User: cees
 * Date: Apr 13, 2007
 * Time: 8:21:01 PM
 *
 * @todo Document this class
 */
public class TexasController extends AbstractJeubelixController
{
   Log log = LogFactory.getLog(TexasController.class);

   TexasGameManager texasGameManager;


   /**
    * @param httpServletRequest the HTTP request
    * @return a {@link nl.ceasaro.poker.texasHoldEm.game.TexasTable} command object.
    * @throws Exception
    */
   protected Object formBackingObject(HttpServletRequest httpServletRequest) throws Exception
   {
      return TexasPokerCmd.class;
   }

   protected ModelAndView showJeubelixForm(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BindException bindException) throws Exception
   {
      log.debug("in showJeubelixForm");
      TexasGameManager texasGameManager = getTexasGameManager();
      texasGameManager.takeSeat(new TexasPlayer("cees"));
      texasGameManager.takeSeat(new TexasPlayer("yasmine"));
      texasGameManager.takeSeat(new TexasPlayer("jessy"));
      texasGameManager.takeSeat(new TexasPlayer("wes lee"));
      ModelAndView view = new ModelAndView("texas.table");
      view.addObject(getTexasGameManager().getTexasTable());
      return view;
   }

   protected ModelAndView processJeubelixForm(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object, BindException bindException) throws Exception
   {
      //TexasPokerCmd texasPokerCmd = (TexasPokerCmd) object;
      //String action = texasPokerCmd.getAction();
      //log.info("action = "+action);
      String action = httpServletRequest.getParameter("action");
      log.info("action parameter = " + action);
      if ("nextRound".equals(action))
      {
         getTexasGameManager().playRound();
      }
      else if ("reset".equals(action))
      {
         getTexasGameManager().reset();
      }
      ModelAndView view = new ModelAndView("texas.table");
      view.addObject(getTexasGameManager().getTexasTable());
      return view;
   }

   public TexasGameManager getTexasGameManager()
   {
      return texasGameManager;
   }

   public void setTexasGameManager(TexasGameManager texasGameManager)
   {
      this.texasGameManager = texasGameManager;
   }
}
